package com.codechenxi.mqtt.handler;

import com.codechenxi.mqtt.service.TbLampService;
import com.codechenxi.mqtt.service.TbLampStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;



/**
 * @author codechenxi
 * @description
 * @since 2024/11/14 16:24
 */
@Component
public class ReceiverMessageHandler implements MessageHandler {


    @Autowired
    private TbLampService tbLampService ;

    @Autowired
    private TbLampStatusService tbLampStatusService;

    @Override
    public void handleMessage(Message<?> message) throws MessagingException {
        String payload = message.getPayload().toString();
        System.out.println(payload);
        System.out.println(message);
        MessageHeaders headers = message.getHeaders();
        String topicName = headers.get("mqtt_receivedTopic").toString();
        if("codechenxi/iot/lamp/line".equals(topicName)) {
            tbLampService.updateLampOnlineStatus(message.getPayload().toString()) ;        // 更新智能灯泡的上线状态
        }else if("codechenxi/iot/lamp/device/status".equals(topicName)) {
            tbLampStatusService.saveDeviceStatus(message.getPayload().toString()) ;
        }
    }

}
